//
//  HLYiTanContentCollectionViewCell.m
//  Various
//
//  Created by HarwordLiu on 16/3/29.
//  Copyright © 2016年 Harword. All rights reserved.
//

#import "HLYiTanContentCollectionViewCell.h"
#import <UIImageView+WebCache.h>

@implementation HLYiTanContentCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatSubView];
    }
    return self;
}


- (void)creatSubView {
    
    
    self.imageViewTitle = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height / 4)];
    [self.contentView addSubview:self.imageViewTitle];
    
    self.imageViewTitleType0 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200 * fHEIGHT, 200 *fHEIGHT)];
    self.imageViewTitleType0.center = CGPointMake(self.frame.size.width / 2, 113 * fHEIGHT);
    self.imageViewTitleType0.layer.cornerRadius = 100 * fHEIGHT;
    self.imageViewTitleType0.clipsToBounds = YES;
    [self.contentView addSubview:self.imageViewTitleType0];
    
    self.textLabelContent = [[HLYiYanLabel alloc] initWithFrame:CGRectMake(10, fHEIGHT * 230, fWEIGHT * 394, fHEIGHT * 330)];
    self.textLabelContent.lineBreakMode = NSLineBreakByClipping;
    self.textLabelContent.font = [UIFont systemFontOfSize:17 * sHEIGHT];
    self.textLabelContent.numberOfLines = 0;
    [self.contentView addSubview:self.textLabelContent];
    
    self.textLabelContentHor = [[UILabel alloc] initWithFrame:CGRectMake(10, fHEIGHT * 230, fWEIGHT * 394, fHEIGHT * 330)];
    self.textLabelContentHor.lineBreakMode = NSLineBreakByClipping;
    self.textLabelContentHor.font = [UIFont fontWithName:@"FZQKBYSJW--GB1-0" size:18 * sHEIGHT];
    self.textLabelContentHor.numberOfLines = 0;
    [self.contentView addSubview:self.textLabelContentHor];
    
    
    self.textLabelAuthor = [[UILabel alloc] initWithFrame:CGRectMake(0, fHEIGHT * 590, fWEIGHT * 414, fHEIGHT * 50)];
    self.textLabelAuthor.textAlignment = NSTextAlignmentCenter;
    self.textLabelAuthor.font = [UIFont boldSystemFontOfSize:19 * sHEIGHT];
    [self.contentView addSubview:self.textLabelAuthor];
    
    self.textLabelInfomation = [[UILabel alloc] initWithFrame:CGRectMake(5, fHEIGHT * 650, fWEIGHT * 409, fHEIGHT * 86)];
    self.textLabelInfomation.textColor = [UIColor colorWithWhite:0.66 alpha:1];
    self.textLabelInfomation.font = [UIFont systemFontOfSize:15 * sHEIGHT];
    self.textLabelInfomation.numberOfLines = 0;
    [self.contentView addSubview:self.textLabelInfomation];
    
    
}

- (void)setModel:(HLYiYanModel *)model {
    if (_model != model) {
        _model = model;
    }
    self.textLabelContent.alpha = 0;
    self.textLabelContentHor.alpha = 0;
    self.imageViewTitle.alpha = 0;
    self.imageViewTitleType0.alpha = 0;
    
    NSString *strLabelType = [model.type substringToIndex:3];
    NSString *strImageType = [model.type substringWithRange:NSMakeRange(4, 1)];
    /**
     *  yyh 文本水平排版
     */
    if ([strLabelType isEqualToString:@"yyh"]) {
        self.textLabelContentHor.attributedText = [self setlineSqace:model.content];
        self.textLabelContentHor.alpha = 1;
    } else if ([strLabelType isEqualToString:@"yyv"]) {
        self.textLabelContent.text = model.content;
        self.textLabelContent.alpha = 1;
    }
    /**
     *  0 image为圆形
     */
    if ([strImageType isEqualToString:@"0"]) {
        [self.imageViewTitleType0 sd_setImageWithURL:[NSURL URLWithString:model.picpath] placeholderImage:[UIImage imageNamed:@"HLPlaceHolder"]];
        self.imageViewTitleType0.alpha = 1;
    } else {
        [self.imageViewTitle sd_setImageWithURL:[NSURL URLWithString:model.picpath] placeholderImage:[UIImage imageNamed:@"HLPlaceHolder"]];
        self.imageViewTitle.alpha = 1;
    }
    
    
    self.textLabelContent.text = model.content;
    
    
    
    self.textLabelAuthor.text = [NSString stringWithFormat:@"- %@ -", model.from];
    self.textLabelInfomation.text = [NSString stringWithFormat:@"#文字\r收藏 %@ 感悟 %@\r来自 %@ 的文集<<%@>>", model.collectcnt, model.commentcnt, model.creatorModel.username, model.originbookModel.bookname];
}

/**
 *  content文章格式处理
 */
- (NSMutableAttributedString *)setlineSqace:(NSString *)str {
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:str];
    
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    style.headIndent = 0;
    style.firstLineHeadIndent = 25 * sHEIGHT;
    style.lineSpacing = 10 * sHEIGHT;
    
    [text addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, str.length)];
    
    return text;
}


@end
